package org.iabako.server.dao.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.mail.IabakoMailHelper;
import org.iabako.shared.entity.business.ClientConversation;
import org.iabako.shared.entity.enumeration.ConversationStatus;
import org.iabako.shared.entity.enumeration.ConversationType;
import org.iabako.shared.tools.GenericTools;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by jose on 12/02/15.
 */

@Repository("clientConversationDAO")
public class ClientConversationDAO extends JpaDAO<Long, ClientConversation> {
    @Override
    protected String getUniqueNumberVerificationQuery() {
        return null;
    }

    @Override
    protected String getLastNumberQuery() {
        return null;
    }

    public void insertByNativeQuery(Long idClient, String email, Serializable iabakoMailHelper) {

        if (!(iabakoMailHelper instanceof IabakoMailHelper)) {
            return;
        }
        IabakoMailHelper helper = (IabakoMailHelper) iabakoMailHelper;

        String nativeQuery = "INSERT INTO CLIENT_CONVERSATION (ID_CLIENT, DATE, TYPE, INTERLOCUTOR, SUBJECT, BODY, STATUS, AUTOMATIC) " +
                "VALUES (:idClient, :date, :type, :interlocutor, :subject, :body, :status, :automatic)";

        entityManager.createNativeQuery(nativeQuery)
                .setParameter("idClient", idClient)
                .setParameter("date", GenericTools.formatDateSql(new Date()))
                .setParameter("type", ConversationType.email.name())
                .setParameter("interlocutor", email)
                .setParameter("subject", helper.getSubject())
                .setParameter("body", ((IabakoMailHelper) iabakoMailHelper).getBody())
                .setParameter("status", ConversationStatus.done.name())
                .setParameter("automatic", true).executeUpdate();
    }
}
